package com.tarena.luban.worker.server.repository.impl;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.tarena.luban.worker.server.bo.WorkerAreaBO;
import com.tarena.luban.worker.server.dao.WorkerAreaDao;
import com.tarena.luban.worker.server.repository.WorkerAreaRepository;
import com.tarena.luban.worker.server.repository.converter.WorkerAreaConverter;
import com.tarena.tp.luban.worker.po.WorkerArea;
import com.tarena.tp.luban.worker.server.dto.param.WorkerAreaParam;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class WorkerAreaRepoImpl implements WorkerAreaRepository {
    @Autowired
    private WorkerAreaDao workerAreaDao;
    @Override
    public void deleteArea(Long userId) {
        workerAreaDao.delete(userId);
    }
    @Autowired
    private WorkerAreaConverter workerAreaConverter;
    @Override
    public void saveWorkerArea(WorkerCreateParam workerCreateParam) {
        List<WorkerAreaParam> workerAreaParams =
                workerCreateParam.getWorkerAreaParams();
        if (CollectionUtils.isNotEmpty(workerAreaParams)){
            for (WorkerAreaParam param : workerAreaParams) {
                param.setUserId(workerCreateParam.getUserId());
            }
        }
        List<WorkerArea> workerAreas = workerAreaConverter.params2pos(workerAreaParams);
        if (workerAreas.size()>0){
            workerAreaDao.insertAreas(workerAreas);
        }
    }

    @Override
    public List<WorkerAreaBO> getAreasByUserId(Long userId) {
        List<WorkerArea> areas=workerAreaDao.selectByUserId(userId);
        return workerAreaConverter.poList2BoList(areas);
    }
}
